$(function () {
    /**
     * 异步请求 授课教室 授课老师 收费标准
     * 根据选择专业得到该专业的所有老师并显示
     */
    $("#showTeacherAndClassroom").change(function(){
        var $val=$("#showTeacherAndClassroom option:selected").val();
        if($val!=0){
            $.ajax({
                type: "POST",
                url: "findcrt",
                data: "courseTypeId="+$val,
                dataType:"json",
                success: function(res){
                    // 添加之前移除
                    $("#classroom_name option").detach();
                    // 将教室信息显示在老师选项框
                    $options = "<option selected=\"selected\" value='0'>【请选择】</option>"
                    $.each(res.classrooms,function(){
                        // 获取父标签添加子标签option
                        $options += "<option value='"+this.classroomId+"'>"+this.classroomName+"</option>";
                    });
                    $("#classroom_name").html($options);

                    $options = "<option selected=\"selected\" value='0'>【请选择】</option>"
                    // 添加之前移除
                    $("#teacher_name option").detach();
                    // 将教师信息显示在老师选项框
                    $.each(res.teachers,function(){
                        // 获取父标签添加子标签option
                        $options += "<option value='"+this.teacherId+"'>"+this.teacherName+"</option>";
                    });
                    $("#teacher_name").html($options);
                }
            });
        }
    });

    /**
     * 年龄范围限制
     */
    $("#minAge").change(function(){
        var $val=$("#minAge option:selected").val();
        var $max=$("#maxAge option:selected").val();
        if ($max==0) {
            $("#maxAge option").detach();
            $options = "<option selected=\"selected\" value='0'>【请选择】</option>"
            for (var i = $val; i <= 16; i++)
                $options += "<option value='" + i + "'>" + i + "</option>";
            $("#maxAge").html($options);
        }
    });
    $("#maxAge").change(function(){
        var $val=$("#maxAge option:selected").val();
        var $min=$("#minAge option:selected").val();
        if ($min==0) {
            $("#minAge option").detach();
            $options = "<option selected=\"selected\" value='0'>【请选择】</option>"
            for (var i = 6; i <= $val; i++)
                $options += "<option value='" + i + "'>" + i + "</option>";
            $("#minAge").html($options);
        }
    });

    /**
     * 异步请求教师授课教室
     * 根据教师专业
     */
    $("#showClassroom").change(function () {
        var $val = $("#showClassroom option:selected").val();
        if ($val != 0) {
            $.ajax({
                type: "POST",
                url: "findClassroom",
                data: "teacherType=" + $val,
                dataType: "json",
                success: function (res) {
                    $("#teacher_classroom option").detach();
                    $options = "<option selected=\"selected\" value='0'>【请选择】</option>"
                    $.each(res.classrooms, function () {
                        $options += "<option value='" + this.classroomId + "'>" + this.classroomName + "</option>";
                    });
                    $("#teacher_classroom").html($options);
                }
            });
        }
    });

    /**
     * 学员与监护人选择其他时显示输入框
     */
    $("#relation").change(function () {
        if($(this).val()=="其他"){
            $("#relationInput").show();
        }else{
            $("#relationInput").hide();
        }
    });
});